package com.gzq.administrator.testthread;

import android.os.AsyncTask;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.squareup.leakcanary.LeakCanary;

import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity {

    private HandlerHelper<MainActivity> handlerHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        Message message=new Message();
//        message.recycle();
//        ArrayList<Integer> arrayList=new ArrayList<>();
//        arrayList.add(1);
//        MyTask myTask=new MyTask(this);
//        myTask.execute("ddd","ddd","dddd");
        LeakCanary.install(this.getApplication());

        handlerHelper=new HandlerHelper<>(this);

        handlerHelper.setOnHandlerResult(new HandlerHelper.HandlerResult<MainActivity>() {
            @Override
            public void handleMessage(Message msg, MainActivity activity) {
                Toast.makeText(activity, (String)msg.obj, Toast.LENGTH_SHORT).show();
            }
        });

        findViewById(R.id.test1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message=new Message();
                message.obj="niha哦哦哦哦哦";
                handlerHelper.sendMessage(message);
            }
        });

        getSupportFragmentManager().beginTransaction().add(R.id.container,new BlankFragment()).commit();

        findViewById(R.id.addFragment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().add(R.id.container,new BlankFragment()).commit();
            }
        });
    }


    private static class MyTask extends AsyncTask<String,Integer,Void>{

        private WeakReference<MainActivity> reference;

        public MyTask(MainActivity activity) {
            this.reference = new WeakReference<>(activity);
        }


        @Override
        protected Void doInBackground(String... strings) {
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            MainActivity mainActivity=reference.get();
            if (mainActivity==null||mainActivity.isFinishing()||mainActivity.isDestroyed()){
                return;
            }

        }

    }
}
